var express = require('express');
var router = express.Router();
var path = require('path')
var formidable = require('formidable')
var userControll = require('../controllerss/userController')

router.get('/', function(req, res, next) {
    res.send('页面信息');
  });
  
router.get('/index',function(req,res,next){
  console.log('主页面')
    userControll.getAllContents(req,res)
});

router.get('/index1',function(req,res,next){
  // console.log('进入路由1')
  userControll.getAllContents1(req,res)
});
router.get('/index2',function(req,res,next){
  // console.log('进入路由1')
  userControll.getAllContents2(req,res)
});

router.post('/write',function(req,res,next){
  userControll.sendOn(req,res)
  
});

router.post('/upload',function(req,res,next){
  var form = new formidable.IncomingForm()
  form.uploadDir = path.join(__dirname,'..','public','upload')
  form.keepExtensions = true
  form.parse(req,function(err,fields,files){
    console.log(files.pickHeader.path.slice(45))
    console.log(fields)
    var userInfo = {aId:fields.aId,picture:files.pickHeader.path.slice(45)}
    userControll.addImgs(userInfo,res)
  })  
});

router.get('/addComments',function(req,res,next){
  console.log('我进入路由啦')
  userControll.addCommets(req,res)
});
//关注文章
router.get('/focusThis',function(req,res,next){
  console.log('我进入路由啦')
  userControll.focusThis(req,res)
});
router.get('/focusDel',function(req,res,next){
  userControll.focusDel(req,res)
});

router.get('/collect',function(req,res,next){
  userControll.collect(req,res)
});

router.get('/calLove',function(req,res,next){
  console.log('wokaishile')
  userControll.calLove(req,res)
});

router.get('/delCollect',function(req,res,next){
  userControll.delCollect(req,res)
});

router.post('/uploadImg', function (req, res, next) {
  //定义一个对象results，用于返回wangeditor
  var results = {
    "errno": 0,
    "data": []
  }
  var form = new formidable.IncomingForm() //创建上传表单对象
  form.uploadDir = path.join(__dirname, "..", "/public/upload") //创建上传文件的路径
  form.keepExtensions = true //设置保留上传文件的扩展名
  //当每个文件上传时都会触发的事件方法，用于多文件上传
  form.on('file', function (err, file) {
    console.log(path.parse(file.path).base)
    results.data.push('http://localhost:3000/upload/' + path.parse(file.path).base)
  })
  form.parse(req, function (err, fields, files) {
    if (err) {
      res.json({
        "errno": -1,
        "data": []
      })
    }
    // res.json(results)
  })
});

router.get('/getArticleInfo',function(req,res,next){
  userControll.getArticleInfo(req,res)
});

router.get('/addCommentss',function(req,res,next){
  userControll.addCommentss(req,res)
});

module.exports = router;